package com.sy.utils;

import com.sy.dto.UserSession;
import com.sy.filter.ServletApiHolder;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Objects;

import static com.sy.constant.BusinessConstant.COOKIE_USER_TOKEN_NAME;
import static com.sy.constant.BusinessConstant.REDIS_USER_KEY_PREFIX;

public interface CommonOperations {
    String COOKIE_ROOT_PATH = "/";
    int COOKIE_MIN_AGE = 0;

    /**
        * 设置或修改Cookie
     * @param name Cookie名称
     * @param value Cookie值
     * @param  age Cookie过期时间
     */
    default void setCookie(String name, String value, Integer age) {
        HttpServletResponse res = ServletApiHolder.getCurRes();
        Cookie cookie = new Cookie(name, value);
        cookie.setPath(COOKIE_ROOT_PATH);
        if (!Objects.isNull(age) && age.intValue() > 0) {
            cookie.setMaxAge(age);
        }
        res.addCookie(cookie);
    }

    /**
     * 根据Cookie的名称获取Cookie的值
     * @param name Cookie的名称
     * @return Cookie的值
     */
    default String getCookie(String name,HttpServletRequest req) {
//        HttpServletRequest req = ServletApiHolder.getCurReq();
        Cookie[] cookies = req.getCookies();
        if (!ArrayUtils.isEmpty(cookies)) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

    /**
     * 删除Cookie
     * @param name Cookie的名字
     */
    default void delCookie(String name,HttpServletRequest req,HttpServletResponse res) {
//        HttpServletRequest req = ServletApiHolder.getCurReq();
//        HttpServletResponse res = ServletApiHolder.getCurRes();
        Cookie[] cookies = req.getCookies();
        if (!Objects.isNull(cookies)) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    cookie.setMaxAge(COOKIE_MIN_AGE);
                    cookie.setPath(COOKIE_ROOT_PATH);
                    res.addCookie(cookie);
                }
            }
        }
    }

//    default UserSession getCurLoginUser() {
//        //从Cookie中获取当前客户端的token
//        String token = getCookie(COOKIE_USER_TOKEN_NAME);
//        //如果token不存在-未登录
//        if (StringUtils.isBlank(token)) {
//            return null;
//        }
//        //如果token存在
//        else {
//            //判断Redis中有无与当前token对应的key
//            String redisUserKey = REDIS_USER_KEY_PREFIX + token;
//            boolean keyExists = REDIS_OPERATIONS.exists(redisUserKey);
//            //1.如果存在，说明用户已经登录
//            if (keyExists) {
//                //将当前的用户信息返回到前端
//                Map<String, String> redisUserMap = REDIS_OPERATIONS.hgetall(redisUserKey);
//                UserSession userSession = CommonUtils.map2Bean(redisUserMap, UserSession.class);
//                return userSession;
//            }
//            //2.如果不存在，说明用户尚未登录
//            else {
//                return null;
//            }
//        }
//    }
    default UserSession getCurLoginUser(RedisOperations redisOperations,HttpServletRequest req) {
        String token = getCookie(COOKIE_USER_TOKEN_NAME,req);
        if (StringUtils.isNotBlank(token)) {
            String redisUserKey = REDIS_USER_KEY_PREFIX + token;
            if (redisOperations.exists(redisUserKey)) {
                Map<String, String> redisUserMap = redisOperations.hgetall(redisUserKey);
                UserSession userSession = CommonUtils.map2Bean(redisUserMap, UserSession.class);
                if (Objects.isNull(userSession)) {
                    return null;
                } else {
                    return userSession;
                }
            }
        }
        return null;
    }



}
